package com.liwbn.rbac.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.liwbn.rbac.model.Role;
import com.liwbn.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.liwbn.rbac.dao.FramemenuMapper;
import com.liwbn.rbac.dao.PermissionMapper;
import com.liwbn.rbac.model.Framemenu;
import com.liwbn.rbac.model.FramemenuExample;
import com.liwbn.rbac.model.PermissionExample;
import com.liwbn.rbac.service.FramemenuService;
import com.liwbn.rbac.vo.SideBar;

@Service
public class FramemenuServiceImpl implements FramemenuService {

    @Autowired
    private FramemenuMapper framemenuMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleService roleService;

    @Override
    public Long insert(Framemenu model) {
        return framemenuMapper.insert(model);
    }

    @Override
    public int insertSelective(Framemenu model) {
        return framemenuMapper.insertSelective(model);
    }

    @Override
    public int updateByPrimaryKey(Framemenu model) {
        return framemenuMapper.updateByPrimaryKey(model);
    }

    @Override
    public int updateByPrimaryKeySelective(Framemenu model) {
        return framemenuMapper.updateByPrimaryKeySelective(model);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return framemenuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Framemenu selectByPrimaryKey(Long id) {
        return framemenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Framemenu> selectAll() {
        FramemenuExample example = new FramemenuExample();
        example.setOrderByClause("orders");
        return framemenuMapper.selectByExample(example);
    }

    @Override
    public List<SideBar> selectSideBar() {
        FramemenuExample example = new FramemenuExample();
        FramemenuExample.Criteria criteria = example.createCriteria();
        criteria.addCriterion("a.parentId is null");
        criteria.addCriterion("a.status=", 1, "a.status");
        criteria.addCriterion("b.status=", 1, "b.status");
        example.setOrderByClause("a.orders, b.orders");
        return framemenuMapper.selectSideBarByExample(example);
    }

    @Override
    public List<SideBar> selectSideBarByRole(String userName) {
        List<SideBar> child = null;
        List<Role> roles = roleService.selectRolesByUserName(userName);
        if (roles.size() == 0) {
            return child;
        }
        List<Long> ids = new ArrayList<>();
        for (Role role : roles) {
            ids.add(role.getId());
        }
        List<Long> menuIds = permissionMapper.selectMenuIdByRoles(ids);
        FramemenuExample example = new FramemenuExample();
        FramemenuExample.Criteria criteria = example.createCriteria();
        criteria.addCriterion("a.parentId is null");
        criteria.addCriterion("a.status=", 1, "a.status");
        criteria.addCriterion("b.status=", 1, "b.status");
        example.setOrderByClause("a.orders, b.orders");
        List<SideBar> sideBars = framemenuMapper
                .selectSideBarByExample(example);
        SideBar c;
        for (SideBar sideBar : sideBars) {
            child = sideBar.getChildren();
            if (child != null && child.size() > 0) {
                for (int i = child.size() - 1; i >= 0; i--) {
                    c = child.get(i);
                    if (!menuIds.contains(c.getId())) {
                        child.remove(i);
                    }
                }
            }
        }
        return sideBars;
    }

    @Override
    public void updateByJSONMenus(List<List<Framemenu>> menus) {
        List<Framemenu> addMenus = menus.get(0);
        List<Framemenu> updateMenus = menus.get(1);
        List<Framemenu> delMenus = menus.get(2);
        if (addMenus.size() > 0) {
            framemenuMapper.insertBatch(addMenus);
        }
        if (updateMenus.size() > 0) {
            for (Framemenu f : updateMenus) {
                framemenuMapper.updateByPrimaryKeySelective(f);
            }
        }
        if (delMenus.size() > 0) {
            List<Long> ids = new ArrayList<>();
            for (Framemenu f : delMenus) {
                ids.add(f.getId());
            }
            framemenuMapper.deleteByPrimaryKeys(ids);
            PermissionExample example = new PermissionExample();
            PermissionExample.Criteria criteria = example.createCriteria();
            criteria.andMenuIdIn(ids);
            permissionMapper.deleteByExample(example);
        }
    }

    @Override
    public List<SideBar> selectParentSideBar() {
        FramemenuExample framemenuExample = new FramemenuExample();
        FramemenuExample.Criteria framemenuCriteria = framemenuExample
                .createCriteria();
        framemenuCriteria.andParentidIsNull();
        framemenuExample.setOrderByClause("orders");
        List<SideBar> parent = framemenuMapper
                .selectParentSideBarByExample(framemenuExample);
        return parent;
    }

}
